package day8_interview.read_write_lock_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
 * 读写锁
 */
public class Main {
    public static void main(String[] args) {

        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        Lock readLock = readWriteLock.readLock();
        Lock writeLock = readWriteLock.writeLock();

        // “写”的角色，请求写锁
        // "只读“角色，请求读锁

        readLock.lock();        // 读锁已经有了
//        writeLock.lock();       // 写锁锁上
        Thread t = new Thread() {
            @Override
            public void run() {
                readLock.lock(); // 主线程已经抢到锁，子线程依然可以抢到锁
//                writeLock.lock();
                System.out.println("子线程也可以加读锁成功");
            }
        };
        t.start();
    }
}
